package com.softhis.rekrutacja.client.view;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.Validator;
import com.extjs.gxt.ui.client.widget.layout.CenterLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.ui.SimplePanel;
import com.softhis.rekrutacja.client.presenter.LoginFormPresenter.LoginFormViewInterface;
import com.softhis.rekrutacja.client.view.widgets.impl.MyGxtButton;
import com.softhis.rekrutacja.client.view.widgets.impl.MyGxtLabel;
import com.softhis.rekrutacja.client.view.widgets.impl.MyGxtTextField;
import com.softhis.rekrutacja.client.view.widgets.interfaces.IButton;
import com.softhis.rekrutacja.client.view.widgets.interfaces.ITextField;

/**
 * @author Sasak
 * 
 */
public class LoginFormView extends ContentPanel implements LoginFormViewInterface {

	private SimplePanel wrap = new SimplePanel();
	private FormPanel loginForm = new FormPanel();
	private MyGxtTextField loginField = new MyGxtTextField();
	private MyGxtTextField passwordField = new MyGxtTextField();
	private MyGxtButton loginButton = new MyGxtButton("Zaloguj");
	private FormData formData = new FormData("-20");
	private LayoutContainer centerWrapper = new LayoutContainer(new CenterLayout());
	private LayoutContainer centerRowWrapper = new LayoutContainer(new RowLayout(Orientation.VERTICAL));
	private MyGxtLabel messagelabel = new MyGxtLabel();
	private ContentPanel loginPanel = new ContentPanel();
	
	// private MyGxtButton newAccountButton = new MyGxtButton();
	// private MyGxtButton remindPasswordButton = new MyGxtButton();
	private ToolBar toolbar = new ToolBar();

	public LoginFormView() {
		
		this.setHeaderVisible(false);
		
		this.loginPanel.setFrame(true);
		this.loginPanel.setHeading("Zaloguj: ");
		this.loginPanel.setHeaderVisible(true);
		this.loginPanel.setWidth(300);
		this.loginPanel.setHeight(200);

		FormLayout formLayout = new FormLayout(FormPanel.LabelAlign.TOP);
		formLayout.setDefaultWidth(200);

		this.loginForm.setLayout(formLayout);
		this.loginForm.setHeaderVisible(false);

		this.loginField.setFieldLabel("Login");
		this.loginField.setAllowBlank(false);
		this.loginField.getMessages().setBlankText("To pole jest wymagane");
		
		this.loginField.setValidator(new Validator() {

			public String validate(Field<?> field, String value) {;
				if (value.length() < 4)
					return "To pole musi zawierać przynajmniej 4 znaki";

				if (value.length() > 30)
					return "To pole nie może być dłuższe niż 30 znaków";

				return null;
			}
		});
		
		this.passwordField.setPassword(true);
		this.passwordField.setFieldLabel("Hasło");
		this.passwordField.setAllowBlank(false);
		this.passwordField.getMessages().setBlankText("To pole jest wymagane");
		this.passwordField.setValidator(new Validator() {

			public String validate(Field<?> field, String value) {
				if (value.isEmpty())
					return "To pole jest wymagane";

				if (value.length() < 6)
					return "To pole musi zawierać przynajmniej 6 znaków";

				if (value.length() > 30)
					return "To pole nie może być dłuższe niż 30 znaków";

				return null;
			}
		});
		

		this.loginForm.add(this.loginField, this.formData);
		this.loginForm.add(this.passwordField, this.formData);
		this.loginForm.setButtonAlign(HorizontalAlignment.CENTER);

		this.toolbar.setAlignment(HorizontalAlignment.CENTER);

		this.loginButton.setWidth(125);

		// this.newAccountButton.setWidth(125);
		// this.toolbar.add(newAccountButton);
		// this.remindPasswordButton.setWidth(125);
		// this.toolbar.add(remindPasswordButton);

		this.toolbar.add(this.loginButton);

		this.wrap.add(this.loginForm);
		this.loginPanel.setButtonAlign(HorizontalAlignment.CENTER);
		
		this.loginPanel.add(wrap);
		this.loginPanel.add(toolbar);
		
		FormButtonBinding loginBind = new FormButtonBinding(loginForm);
		loginBind.addButton(loginButton);
		this.messagelabel.setWidth(300);
		this.messagelabel.setAutoWidth(false);
		
		this.centerWrapper.setHeight(300);
		this.centerRowWrapper.add(messagelabel, new RowData(1, -1, new Margins(10)));
		this.centerRowWrapper.add(loginPanel, new RowData(1, 1, new Margins(10, 4, 0, 4)));
		this.centerWrapper.add(centerRowWrapper);
		
		this.add(centerWrapper);
	}

	public ITextField getLoginField() {
		return this.loginField;
	}

	public ITextField getPasswordField() {
		return this.passwordField;
	}

	public IButton getLoginButton() {
		return this.loginButton;
	}

	public void clearForm() {
		this.messagelabel.setText("");
		this.loginForm.clear();
	}

	public boolean isFormValid() {
		return this.loginForm.isValid();
	}
	
    public void setMessage(String message) {
    	this.messagelabel.setText(message);
    }
    
    public void setWarning(String warning) {
    	this.messagelabel.setText("<span style='color: red'>"+warning+"</span>");
    }

}
